package com.lixun.passwordbook.provider;

import com.lixun.passwordbook.ResourceTable;
import com.lixun.passwordbook.pojo.Colors;
import com.lixun.passwordbook.pojo.Data;
import com.lixun.passwordbook.slice.MainAbilitySlice;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

public class Provider extends BaseItemProvider {
    private List<Data> list;
    private MainAbilitySlice slice;
    private Context mContext;
    public Provider(List<Data> list, MainAbilitySlice slice,Context context) {
        this.list = list;
        this.slice = slice;
        this.mContext = context;
    }
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }
    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()){
            return list.get(position);
        }
        return null;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_home, null, false);
        } else {
            cpt = convertComponent;
        }
        boolean dark_mode = Colors.getDarkMode();
        ShapeElement element;
        Color color;
        if (dark_mode){
            element = new ShapeElement(mContext,ResourceTable.Graphic_item_background_dark);
            color = Colors.textColorDark;
        } else {
            element = new ShapeElement(mContext,ResourceTable.Graphic_item_background);
            color = Colors.textColor;
        }
        cpt.setBackground(element);
        Data data = list.get(position);
        Text text = (Text) cpt.findComponentById(ResourceTable.Id_nickname);
        text.setText(data.getNickname());
        text.setTextColor(color);
        Text text1 = (Text) cpt.findComponentById(ResourceTable.Id_type);
        text1.setText(data.getType());
        text1.setTextColor(color);
        Text text2 = (Text) cpt.findComponentById(ResourceTable.Id_view);
        text2.setText(data.getModifytime());
        text2.setTextColor(color);
        return cpt;
    }
}
